package com.lvwenguo.controller.menujump;

import cn.itcast.commons.CommonUtils;
import com.lvwenguo.model.CategoryList;
import com.lvwenguo.service.PurchaseServiceInter;
import com.lvwenguo.service.SaleInter;
import com.lvwenguo.service.StockInter;
import com.lvwenguo.service.UserServiceInter;
import com.lvwenguo.service.serviceImple.PurchaseServiceImple;
import com.lvwenguo.service.serviceImple.SaleImple;
import com.lvwenguo.service.serviceImple.StockImple;
import com.lvwenguo.service.serviceImple.UserServiceImple;
import com.lvwenguo.utils.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//主要控制菜单跳转页面，数据显示
@WebServlet(urlPatterns = "/MenuJumpServlet")
public class MenuJumpServlet extends BaseServlet {
    PurchaseServiceInter purchaseServiceImper=new PurchaseServiceImple();
    SaleInter saleInter=new SaleImple();
    StockInter stockInter=new StockImple();
    UserServiceInter userServiceInter=new UserServiceImple();
    /*
    * 用于品类修改跳转页面，回显数据
    * */
    public void category(HttpServletRequest request,HttpServletResponse response){
        CategoryList categoryList = CommonUtils.toBean(request.getParameterMap(), CategoryList.class);
        request.setAttribute("categoryList",categoryList);
        try {
            request.getRequestDispatcher("/view/category/category_modify.jsp").forward(request,response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /*
    * 用于(进货记录)菜单跳转页面
    * */
    public void purchase(HttpServletRequest request, HttpServletResponse response){

        try {
                request.setAttribute("Category",purchaseServiceImper.selectCategoryPurchaseList());
            request.getRequestDispatcher("/view/purchase/purchase_list.jsp").forward(request,response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
    //用于（供应商）菜单页面跳转
    public void supplier(HttpServletRequest request, HttpServletResponse response){

        try {
                request.setAttribute("supplier",purchaseServiceImper.selectSupplier());
            System.out.println(request.getAttribute("supplier"));
            request.getRequestDispatcher("/view/supplier/supplier_list.jsp").forward(request,response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //用于（销售报表）菜单
    public void sale_list(HttpServletRequest request, HttpServletResponse response){

        try {
            request.setAttribute("sale",saleInter.selectSale());
            request.getRequestDispatcher("/view/sale/sale_list.jsp").forward(request,response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //用于（库存列表）菜单
    public void stock_list(HttpServletRequest request, HttpServletResponse response){

        try {
            request.setAttribute("stock",stockInter.selectStock(0));
            request.getRequestDispatcher("/view/stock/stock_list.jsp").forward(request,response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //用于（变更记录）菜单
    public void stock_recorder_list(HttpServletRequest request, HttpServletResponse response){

        try {
            request.setAttribute("stock",stockInter.selectStock(1));
            request.getRequestDispatcher("/view/stock/stock_recorder_list.jsp").forward(request,response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //用于（用户管理）菜单
    public void user_list(HttpServletRequest request, HttpServletResponse response){

        try {
                request.setAttribute("user",userServiceInter.select());
                request.getRequestDispatcher("/view/user/user_list.jsp").forward(request,response);

        }  catch (Exception e) {
            e.printStackTrace();
        }

    }
    //用于权限管理
    public void user_rose(HttpServletRequest request, HttpServletResponse response) {

        try {
            Object role = request.getSession().getAttribute("role");
            if (role.equals("管理员")) {
                request.setAttribute("user", userServiceInter.select());
                response.getWriter().print(1);
                //request.getRequestDispatcher("/view/user/user_list.jsp").forward(request,response);
            } else {
                response.getWriter().print(0);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
